
'use strict';
angular.module('app')
	.controller('activityController',['$scope','ajaxRquest','$state', function($scope,ajaxRquest,$state) {
		var huiToken =$.cookie("huiToken");
		var uid = $scope.getUrlParam("uid");
		var phone = $scope.getUrlParam("phone");
		var platform = $scope.getUrlParam("platform");
		var webType = $scope.getUrlParam("webType");
		var channel = $scope.getUrlParam("channel");
        $scope.isShow=false
        if(phone){
        	$.cookie("uid",uid);
		    $.cookie("phone",phone);
	     	$.cookie("platform",platform);
	    	$.cookie("webType",webType);
        }else{
        	platform = 'hyz';
        	phone = $.cookie("phone")
        }
		$.cookie("channel",channel);

		//校验是否注册慧驿站接口– 王坤
		$scope.isRegistered=function(){
			ajaxRquest.getData('/userCenter/isRegistered',{
				mobile:phone, //	String	手机号	是
			}).then(function(res){
				if(res.resCode === "00000"){
					var isRegisteredFlag=res.record.isRegisteredFlag;
					$.cookie("isRegisteredFlag",isRegisteredFlag);
					if(isRegisteredFlag=="N"){
						$state.go("access.index");
					}else{
						$scope.$apply(function(){
							$scope.isShow=true
						})
						$scope.isRegisteredUC()
					}
				}else{
					$scope.formError({text:res.resMsg});
				}
			})
		}

       //校验是否授权UC接口– 王坤
        $scope.isRegisteredUC=function(){
			ajaxRquest.getData('/userCenter/isRegisteredUC',{
				mobile:$.cookie("phone"), //	String	手机号	是
				channel:platform,         //   String  渠道(hyz-慧驿站，lbjr-金融，lbn-老板娘，ld-铃铛，zqh-智仟汇)  是
			}).then(function(res){
				if(res.resCode === "00000"){
					var isLoginedFlag=res.record.isLoginedFlag;
					if(isLoginedFlag=="N"){

						$state.go("access.index");

					}else{
						$.cookie('huiToken',res.record.huiToken)
						$scope.througthCheck()
					}
				}else{
					$scope.formError({text:res.resMsg});
				}
			})
        }

/*		document.addEventListener('visibilitychange',function()
		{
			if(document.webkitVisibilityState !='hidden')
			{
				if(phone){
					$scope.isRegistered();
				}else{
					$state.go("access.login");
				}
			}
		})
		document.addEventListener('webkitvisibilitychange',function()
		{
			if(document.webkitVisibilityState !='hidden')
			{
				if(phone){
					$scope.isRegistered();
				}else{
					$state.go("access.login");
				}
			}
		})
		document.addEventListener('mozvisibilitychange',function()
		{
			if(document.mozVisibilityState !='hidden')
			{
				if(phone){
					$scope.isRegistered();
				}else{
					$state.go("access.login");
				}
			}
		})*/





		if(phone){
			$scope.isRegistered();
		}else{
			$state.go("access.login");
		}
        $scope.througthCheck=function(){
	        ajaxRquest.getData("/funScenes/recommendactivity",{}).then(function(res){
				if(res.resCode === "00000"){
					$scope.$apply(function(){
						$scope.record = res.record;
					})
				}else{
					$scope.formError({
						text:res.resMsg, //内容
					})
				}
			})
        }


		$scope.openRunChangjing =function(){
			var huiToken =$.cookie("huiToken")
			window.location.href = "/iFengChao-Run-UI/#/access/index?huiToken="+huiToken+"&phone="+phone
		}
		$scope.goToDetail=function(activityId){
			var huiToken=$.cookie('huiToken');
			var mobile=$.cookie("phone")
			window.location.href = "/iFengChao-Run-UI/#/access/activeDetail/"+activityId+"/"+huiToken+"/"+mobile

		}
} ]);
